home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.0.14 / python / xpcom / server / module.py < prev    next >
Encoding:
Python Source  |  2006-06-06  |  4.7 KB  |  114 lines

  1. # ***** BEGIN LICENSE BLOCK *****
  2. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. #
  4. # The contents of this file are subject to the Mozilla Public License Version
  5. # 1.1 (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. # http://www.mozilla.org/MPL/
  8. #
  9. # Software distributed under the License is distributed on an "AS IS" basis,
  10. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. # for the specific language governing rights and limitations under the
  12. # License.
  13. #
  14. # The Original Code is the Python XPCOM language bindings.
  15. #
  16. # The Initial Developer of the Original Code is ActiveState Tool Corp.
  17. # Portions created by ActiveState Tool Corp. are Copyright (C) 2000, 2001
  18. # ActiveState Tool Corp.  All Rights Reserved.
  19. #
  20. # Contributor(s):
  21. #   Mark Hammond <MarkH@ActiveState.com> (original author)
  22. #
  23. # Alternatively, the contents of this file may be used under the terms of
  24. # either the GNU General Public License Version 2 or later (the "GPL"), or
  25. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. # in which case the provisions of the GPL or the LGPL are applicable instead
  27. # of those above. If you wish to allow use of your version of this file only
  28. # under the terms of either the GPL or the LGPL, and not to allow others to
  29. # use your version of this file under the terms of the MPL, indicate your
  30. # decision by deleting the provisions above and replace them with the notice
  31. # and other provisions required by the GPL or the LGPL. If you do not delete
  32. # the provisions above, a recipient may use your version of this file under
  33. # the terms of any one of the MPL, the GPL or the LGPL.
  34. #
  35. # ***** END LICENSE BLOCK *****
  36.  
  37. from xpcom import components
  38. from xpcom import ServerException, Exception
  39. from xpcom import nsError
  40.  
  41. import factory
  42.  
  43. import types
  44. import os
  45.  
  46. class Module:
  47.     _com_interfaces_ = components.interfaces.nsIModule
  48.     def __init__(self, comps):
  49.         # Build a map of classes we can provide factories for.
  50.         c = self.components = {}
  51.         for klass in comps:
  52.             c[components.ID(klass._reg_clsid_)] = klass
  53.         self.klassFactory = factory.Factory
  54.  
  55.     def getClassObject(self, compMgr, clsid, iid):
  56.         # Single retval result.
  57.         try:
  58.             klass = self.components[clsid]
  59.         except KeyError:
  60.             raise ServerException(nsError.NS_ERROR_FACTORY_NOT_REGISTERED)
  61.         
  62.         # We can ignore the IID - the auto-wrap process will automatically QI us.
  63.         return self.klassFactory(klass)
  64.  
  65.     def registerSelf(self, compMgr, location, loaderStr, componentType):
  66.         # void function.
  67.         fname = os.path.basename(location.path)
  68.         for klass in self.components.values():
  69.             reg_contractid = klass._reg_contractid_
  70.             try:
  71.                 print "Registering '%s' (%s)" % (reg_contractid, fname)
  72.             except IOError:
  73.                 pass # stdout may not be valid for windows apps!
  74.             reg_desc = getattr(klass, "_reg_desc_", reg_contractid)
  75.             compMgr = compMgr.queryInterface(components.interfaces.nsIComponentRegistrar)
  76.             compMgr.registerFactoryLocation(klass._reg_clsid_,
  77.                                               reg_desc,
  78.                                               reg_contractid,
  79.                                               location,
  80.                                               loaderStr,
  81.                                               componentType)
  82.  
  83.             # See if this class nominates custom register_self
  84.             extra_func = getattr(klass, "_reg_registrar_", (None,None))[0]
  85.             if extra_func is not None:
  86.                 extra_func(klass, compMgr, location, loaderStr, componentType)
  87.  
  88.     def unregisterSelf(self, compMgr, location, loaderStr):
  89.         # void function.
  90.         for klass in self.components.values():
  91.             ok = 1
  92.             try:
  93.                 compMgr.unregisterComponentSpec(klass._reg_clsid_, location)
  94.             except Exception:
  95.                 ok = 0
  96.             # Give the class a bash even if we failed!
  97.             extra_func = getattr(klass, "_reg_registrar_", (None,None))[1]
  98.             if extra_func is not None:
  99.                 try:
  100.                     extra_func(klass, compMgr, location, loaderStr)
  101.                 except Exception:
  102.                     ok = 0
  103.             try:
  104.                 if ok:
  105.                     print "Successfully unregistered", klass.__name__
  106.                 else:
  107.                     print "Unregistration of", klass.__name__, "failed (not previously registered?)"
  108.             except IOError:
  109.                 pass
  110.  
  111.     def canUnload(self, compMgr):
  112.         # single bool result
  113.         return 0 # we can never unload!
  114.